//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.util;

import com.domain.TextMessage;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WxMessageUtil {
    private static final Logger log = LoggerFactory.getLogger(WxMessageUtil.class);

    public WxMessageUtil() {
    }

    public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException {
        HashMap<String, String> map = new HashMap();
        SAXReader reader = new SAXReader();
        InputStream ins = request.getInputStream();
        Document doc = reader.read(ins);
        Element root = doc.getRootElement();
        List<Element> list = root.elements();
        Iterator var7 = list.iterator();

        while(var7.hasNext()) {
            Element e = (Element)var7.next();
            map.put(e.getName(), e.getText());
        }

        ins.close();
        return map;
    }

    public static String getWxReturnMsg(Map<String, String> decryptMap, String content) throws UnsupportedEncodingException {
        log.info("---开始封装xml---decryptMap:" + decryptMap.toString());
        TextMessage textMessage = new TextMessage();
        textMessage.setToUserName((String)decryptMap.get("FromUserName"));
        textMessage.setFromUserName((String)decryptMap.get("ToUserName"));
        textMessage.setCreateTime(System.currentTimeMillis());
        textMessage.setMsgType("text");
        textMessage.setContent(content);
        String xmlMsg = getXmlString(textMessage);
        String encodeXmlMsg = new String(xmlMsg.getBytes(), "ISO-8859-1");
        return encodeXmlMsg;
    }

    private static String getXmlString(TextMessage textMessage) {
        String xml = "";
        if (textMessage != null) {
            xml = "<xml>";
            xml = xml + "<ToUserName><![CDATA[";
            xml = xml + textMessage.getToUserName();
            xml = xml + "]]></ToUserName>";
            xml = xml + "<FromUserName><![CDATA[";
            xml = xml + textMessage.getFromUserName();
            xml = xml + "]]></FromUserName>";
            xml = xml + "<CreateTime>";
            xml = xml + textMessage.getCreateTime();
            xml = xml + "</CreateTime>";
            xml = xml + "<MsgType><![CDATA[";
            xml = xml + textMessage.getMsgType();
            xml = xml + "]]></MsgType>";
            xml = xml + "<Content><![CDATA[";
            xml = xml + textMessage.getContent();
            xml = xml + "]]></Content>";
            xml = xml + "</xml>";
        }

        log.info("xml封装结果=>" + xml);
        return xml;
    }
}
